import wxvalidate from'wechat-validate'
Page({
  behaviors: [wxvalidate],
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    minDate: new Date().getTime(),
    maxDate: new Date().getTime()+1000*3600*24*3,
    houseList: [],
    houseId:"",
    housename:'',
    name:"",
    gender:"",
    mobile:"",
    visitDate:'',
    currentDate:new Date().getTime(),
  },
  rules: {
    name: [
      {required: true, message: '访客姓名不能为空!'},
      {pattern: /[\u4e00-\u9fa5]{2,5}/, message: '访客姓名只能为中文!'},
    ],
    mobile: [
      {required: true, message: '访客手机号不能为空!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    houseId: [
      {required: true, message: '请选择到访的房屋!'}
    ],
    visitDate: [
      {required: true, message: '请选择到访的日期!'}
    ],
  },
  onLoad() {
    this.getHouseLIst()
  },
  async getHouseLIst() {
    const { data: houseList, code } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast()
    // console.log(data);
    this.setData({
      houseList,
    })
  },
  selectHouseInfo(ev) {
    console.log(ev.detail)
    this.setData({ houseId: ev.detail.id, housename: ev.detail.name })
  },
  async getRepairItem() {
    const { data: repairItem, code } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    // console.log(data);
    this.setData({
      repairItem,
    })
  },
  done(ev){
console.log(ev.detail);
// const data=ev.detail
this.setData({visitDate: wx.utils.dataformate(ev.detail), dateLayerVisible: false })
  },
 async submit(){
  if(!this.validate()) return
  const { name, gender, mobile, houseId, visitDate } = this.data
   const {data:{id},code}=await wx.http.post('/visitor',{houseId,name,gender,mobile,visitDate})
  // console.log(data);
   if(code!==10000) return wx.utils.toast()
   wx.reLaunch({
     url: '/visitor_pkg/pages/passport/index?id='+id,
   })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  goPassport() {
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index',
    })
  },
})
